describe("防疫-消毒任务", () => {
  beforeEach(() => {
    const username = "admin";
    const password = "123456";
    cy.login(username, password);

    cy.intercept("POST", "/api/robot-archives/v1/getCompanyRobotList").as(
      "getCompanyRobotList"
    );

    getTaskList();
    cy.visit("/disinfect/task-manage");

    cy.wait("@getCompanyRobotList");
  });

  it("进入消毒任务-页面显示", () => {
    cy.wait("@getTaskList");
    // 判断页面表头信息是否正确
    const tableTh = [
      "任务名称",
      "机器人编码",
      "途中消毒方式",
      "目标消毒方式",
      "雾化剂量",
      "停留时间",
      "执行时间",
      "紫外线安全保护",
      "工作周期",
      "任务状态",
      "激活状态",
      "操作",
    ];
    cy.get("table>thead>tr>th").should("have.length", tableTh.length);
    for (let i = 0; i < tableTh.length; i++) {
      cy.contains(tableTh[i]);
    }
    // 判断表格数据条数是否大于0
    cy.get("table>tbody>tr").then(($tr) => {
      expect($tr.length).to.greaterThan(0);
    });

    cy.contains("编辑");
    cy.contains("激活");
    cy.contains("删除");
  });

  context("进入消毒任务", () => {
    it.only("添加消毒任务", () => {
      const { name, m, n, time, times, postApi, apiParameter } = {
        name: "测试1",
        m: 0,
        n: 1,
        time: 20,
        times: "工作日",
        postApi: "/api/robot-disinfect-task/v1/add",
        apiParameter: "POST",
      };
      cy.contains("添加任务").click();
      handleTask(name, m, n, time, times, postApi, apiParameter);
      cy.wait("@getTaskList");
      // 判断添加后的列表第一条数据的name
      cy.getByData("my-table").find("tbody>tr").first().contains(name);
    });

    it("编辑消毒任务", () => {
      const { name, m, n, time, times, postApi, apiParameter } = {
        name: "测试2",
        m: 1,
        n: 2,
        time: 30,
        times: "每天",
        postApi: "/api/robot-disinfect-task/v1/update",
        apiParameter: "PUT",
      };

      cy.getByData("handle-btn").eq(0).contains("编辑").click();
      handleTask(name, m, n, time, times, postApi, apiParameter);
      cy.wait("@getTaskList");
      // 判断修改后的列表第一条数据的name
      cy.getByData("my-table").find("tbody>tr").first().contains(name);
    });
  });

  it("激活任务", () => {
    cy.intercept("GET", "/api/robot-disinfect-task/activation/*").as(
      "getActiveTask"
    );
    cy.contains("编辑").siblings().contains("激活").as("activeBtn").click();
    getTaskList();
    cy.contains("警告").closest(".el-message-box").contains("确定").click();
    cy.get("@activeBtn").should("be.hidden");
    cy.contains("操作成功");
    cy.wait("@getTaskList");
  });

  it("删除任务", () => {
    cy.intercept("DELETE", "/api/robot-disinfect-task/v1/*").as("delTask");
    cy.getByData("handle-btn").contains("删除").click();
    getTaskList();
    cy.contains("警告").closest(".el-message-box").contains("确定").click();
    cy.wait("@delTask")
      .its("response.body")
      .then(($res) => {
        const message = $res.message;
        cy.contains(`${message}`);
      });
    cy.wait("@getTaskList");
  });
});

function getTaskList() {
  cy.intercept("GET", "/api/robot-disinfect-task/v1/list*").as("getTaskList");
}

function getElement(e) {
  return cy.contains("label", e).siblings();
}

function handleTask(name, m, n, time, times, postApi, apiParameter) {
  getElement("任务名称").find("input").clear();
  getElement("任务名称").find("input").type(name);
  // 选择防疫机器人
  getElement("防疫机器人").click();
  // 判断数据是否大于0
  cy.getByData("robot-list").should(($li) => {
    expect($li.length).to.be.greaterThan(0);
  });
  cy.getByData("robot-list").eq(m).click();
  // 判断选择后不为空
  getElement("防疫机器人").find("input").should("not.have.value", "");
  // 选择消毒路径
  getElement("消毒路径").as("disinfectionPathSelect").click();
  // 判断数据是否大于0
  cy.getByData("disinfection-path-list").should(($li) => {
    expect($li.length).to.be.greaterThan(0);
  });
  cy.getByData("disinfection-path-list").first().click();
  cy.getByData("disinfection-path-list").eq(n).click();
  cy.get("@disinfectionPathSelect").click();
  // 判断选择后不为空
  cy.get("@disinfectionPathSelect").find("span").should("not.have.text", "");
  getElement("途中消毒方式").find("label").contains("关闭").click();
  getElement("目标消毒方式").find("label").contains("紫外线").click();
  getElement("雾化剂量")
    .find(".el-slider__runway")
    .trigger("mousedown", 0, 0)
    .trigger("mousemove", 40, 0)
    .trigger("mouseup");

  getElement("停留时间").find("input").clear();
  getElement("停留时间").find("input").type(time);

  getElement("紫外线安全保护").contains("是").siblings().click();

  getElement("任务执行时间").find("input").as("getTaskTime").click();
  cy.contains("此刻").click();
  cy.get("@getTaskTime").should("not.have.value", "");

  getElement("任务激活条件").find("input").as("activeTask").click();
  cy.get("ul>li").contains(times).click();
  cy.get("@activeTask").should("not.have.value", "");

  //拦截请求
  cy.intercept(apiParameter, postApi).as("handleTask");
  getTaskList();

  cy.contains("确定").click();

  cy.wait("@handleTask");
  cy.contains("操作成功");
}
